package com.jiawa.train.member.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.jwt.JWTUtil;
import com.jiawa.train.common.exception.BusinessException;
import com.jiawa.train.common.exception.BusinessExceptionEnum;
import com.jiawa.train.common.util.SnowUtil;
import com.jiawa.train.member.domain.Member;
import com.jiawa.train.member.domain.MemberExample;
import com.jiawa.train.member.mapper.MemberMapper;
import com.jiawa.train.member.req.MemberLoginReq;
import com.jiawa.train.member.req.MemberRegisterReq;
import com.jiawa.train.member.req.MemberSendCodeReq;
import com.jiawa.train.member.resp.MemberLoginResp;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service
public class MemberService {
    @Resource
    private MemberMapper mapper;


    public int count() {
        return Math.toIntExact(mapper.countByExample(null));
    }

    public long register(MemberRegisterReq req) {
        String mobile = req.getMobile();
        Member members = getMembers(mobile);
        if (ObjectUtil.isNull(members)) {
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_EXIST);
        }
        Member member = new Member();
        member.setId(SnowUtil.getSnowflakeNextId());
        member.setMobile(mobile);
        mapper.insert(member);


        return member.getId();
    }

    public void sendCode(MemberSendCodeReq req) {
        String mobile = req.getMobile();
        Member members = getMembers(mobile);
        //如果手机号不存在 则插入一条记录
        if (ObjectUtil.isNull(members)) {
            Member member = new Member();
            member.setId(SnowUtil.getSnowflakeNextId());
            member.setMobile(mobile);
            mapper.insert(member);
        }
        //生成验证码
//        String s = RandomUtil.randomString(4);
        String s = "8888";
        //保存短信记录表 :手机号  短信验证码  有效期  是否已使用  业务类型
//        redisTemplate.opsForValue().set(mobile, s, 60, TimeUnit.SECONDS);
        //对接短信通道  发送短信
    }

    public MemberLoginResp login(MemberLoginReq req) {
        String mobile = req.getMobile();
        String code = req.getCode();
        Member members = getMembers(mobile);
        //如果手机号不存在 则插入一条记录
        if (ObjectUtil.isNull(members)) {
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_NOT_EXIST);
        }
        //校验短信认证
        if (!"8888".equals(code)){
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_CODE_ERROR);
        }
        MemberLoginResp memberLoginReq = BeanUtil.copyProperties(members, MemberLoginResp.class);
        Map<String, Object> map = BeanUtil.beanToMap(memberLoginReq);
        //密钥
        String key = "Jiawa12306";
        //生成token
        String token = JWTUtil.createToken(map, key.getBytes());
        memberLoginReq.setToken(token);
        return memberLoginReq;
    }

    private Member getMembers(String mobile) {
        MemberExample memberExample = new MemberExample();
        memberExample.createCriteria().andMobileEqualTo(mobile);
        List<Member> members = mapper.selectByExample(memberExample);
        if (CollUtil.isEmpty(members)) {
            return null;
        } else {
            return members.get(0);
        }
    }

}
